package com.business.activiti.controller;

import com.business.activiti.model.auto.AppealInfo;
import com.business.activiti.model.auto.TaskNode;
import com.business.activiti.service.AppealInfoService;
import com.business.activiti.service.TaskNodeService;
import com.business.form.model.auto.FormInstance;
import com.business.form.model.auto.FormInstanceFieldValue;
import com.business.form.model.auto.FormInstanceFieldValueExample;
import com.business.form.service.FormInstanceFieldValueService;
import com.business.form.service.FormInstanceService;
import com.business.system.common.base.BaseController;
import com.business.system.common.domain.AjaxResult;
import com.business.system.model.custom.TableSplitResult;
import com.business.system.model.custom.Tablepar;
import com.business.system.model.custom.TitleVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("AppealInfoController")
@Api(value = "诉求信息")
public class AppealInfoController extends BaseController{
	
	private String prefix = "gen/appealInfo";
	@Autowired
	private AppealInfoService appealInfoService;
	@Autowired
	private FormInstanceService formInstanceService;
	@Autowired
	private FormInstanceFieldValueService formInstanceFieldValueService;
	@Autowired
	private TaskNodeService taskNodeService;


	@GetMapping("formFieldValueview/{formId}/{taskId}")
	public String formFieldValueview(ModelMap model,@PathVariable("formId")String formId,@PathVariable("taskId")String taskId)
	{
		//根据任务iD查询到自定义的任务节点对象
		TaskNode taskNode = taskNodeService.selectOneByTaskId(taskId);

		model.put("formKey",formId);
		model.put("formInstanceId",taskNode!=null?taskNode.getFormInstanceId() : "");
		return prefix + "/formFieldValue";
	}

	/**
	 * 通过表单实例id展示表单详情
	 * @param model
	 * @param req
	 * @return
	 */
	@GetMapping("/formInstanceView")
	public String formInstanceView(ModelMap model, HttpServletRequest req) {
		String formInstanceId=req.getParameter("formInstanceId");
		FormInstance fi=formInstanceService.selectByPrimaryKey(formInstanceId);
		FormInstanceFieldValueExample example=new FormInstanceFieldValueExample();
		example.createCriteria().andFormInstanceIdEqualTo(formInstanceId);
		List<FormInstanceFieldValue> list=formInstanceFieldValueService.selectByExample(example);
		model.put("FormInstance",fi);
		model.put("FormInstanceFieldValueList",list);
		return prefix + "/formInstanceView";
	}
	@GetMapping("view")
    public String view(ModelMap model)
    {	
		String str="诉求信息";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	/*
	查询诉求信息对应的任务列表页面跳转
	 */
	@GetMapping("appealTaskView/{proInsId}/{appealId}")
	public String appealTaskList(@PathVariable("proInsId")String proInsId,@PathVariable("appealId")String appealId, ModelMap model)
	{
		model.put("proInsId",proInsId);
		model.put("appealId",appealId);
		return prefix + "/appealInfoTaskList";
	}
	//@Log(title = "诉求信息集合查询", action = "111")
	@RequestMapping("list")
	@ResponseBody
	public Object list(Tablepar tablepar, String searchTxt){
		PageInfo<AppealInfo> page=appealInfoService.list(tablepar,searchTxt) ;
		TableSplitResult<AppealInfo> result=new TableSplitResult<AppealInfo>(page.getPageNum(), page.getTotal(), page.getList());
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "诉求信息新增", action = "111")
	@PostMapping("add")
	@ResponseBody
	public AjaxResult add(AppealInfo appealInfo){
		int b=appealInfoService.insertSelective(appealInfo);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	//@Log(title = "诉求信息删除", action = "111")
	@PostMapping("remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=appealInfoService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查用户
	 * @param tsysUser
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(AppealInfo appealInfo){
		int b=appealInfoService.checkNameUnique(appealInfo);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("AppealInfo", appealInfoService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "诉求信息修改", action = "111")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppealInfo record)
    {
        return toAjax(appealInfoService.updateByPrimaryKeySelective(record));
    }

    
    

	
}
